package com.ljx.List;

import com.ljx.Dynamic.ListNode;

/**
 * @author 李加喜
 * @Package com.ljx.List
 * @date 2021-11-06 10:19
 */
// 删除链表第N个节点
    // 思路和查找第N个节点一样  只不过这里是查找第N+1个节点
public class DeleteNList {
    public static void main(String[] args) {

    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dump = new ListNode(-1);
        dump.next=head;
        ListNode x=getKthFromEnd(dump,n+1);
        x.next=x.next.next;
        return dump.next;
    }
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode p1=head;
        ListNode p2=head;
        for (int i=0;i<k;i++){
            p1=p1.next;
        }
        while (p1!=null && p2!=null){
            p1=p1.next;
            p2=p2.next;
        }
        return p2;
    }
}
